package tienmanh.gs.goldminer.sprite;

import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.LoopEntityModifier;
import org.andengine.entity.modifier.PathModifier;
import org.andengine.entity.modifier.SequenceEntityModifier;
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.math.MathUtils;
import org.andengine.util.modifier.IModifier;

import tienmanh.gs.goldminer.manager.ResourcesManager;
import tienmanh.gs.goldminer.scene.GameScene;
import tienmanh.gs.goldminer.util.Value;

public class Mouse extends Object{

	private  boolean isCarry;
	private  int value;
	public static  boolean isDetach;
	private  boolean isHuong;
	private Gold sprite;
	private VertexBufferObjectManager vbom;
	public boolean left , right;
	public float start1,start2,end1,end2,speed;
	private  boolean isCheckMouseRun;
	private boolean vacham;
		
		  @Override
		protected void onManagedUpdate(float pSecondsElapsed) {
			// TODO Auto-generated method stub
			super.onManagedUpdate(pSecondsElapsed);
			if(!isCarry)
			for (int i = 0; i < GameScene.arrayGolds.size(); i++) {
				if(this.collidesWith(GameScene.arrayGolds.get(i))){
					if(GameScene.arrayGolds.get(i).getType().equals(new Value().TYPE_GOLD_2) ||
							    GameScene.arrayGolds.get(i).getType().equals(new Value().TYPE_GOLD_5) || 
							    		GameScene.arrayGolds.get(i).getType().equals(new Value().TYPE_STONE_2) ||
							    			GameScene.arrayGolds.get(i).getType().equals(new Value().TYPE_JEWEL_0)||
							    			GameScene.arrayGolds.get(i).getType().equals(new Value().TYPE_JEWEL_1)||
							    			GameScene.arrayGolds.get(i).getType().equals(new Value().TYPE_JEWEL_2)||
							    			GameScene.arrayGolds.get(i).getType().equals(new Value().TYPE_JEWEL_3)){
						
						this.setGold(getGold() + GameScene.arrayGolds.get(i).getGold());
						isCarry = true;
						isDetach = true;
						this.sprite = new Gold(GameScene.arrayGolds.get(i).getX(), GameScene.arrayGolds.get(i).getY(),30,30, GameScene.arrayGolds.get(i).getTiledTextureRegion(),vbom,0, 0, "");
						GameScene.getInstace().sceneHUD.detachChild(GameScene.arrayGolds.get(i));
						GameScene.getInstace().sceneHUD.attachChild(GameScene.arrayGolds.get(i));
						value = i;
						GameScene.sceneHUD.detachChild(GameScene.arrayGolds.get(value));
//						GameScene.arrayGolds.remove(value);
						GameScene.arrayGolds.set(value, null);
						GameScene.sceneHUD.attachChild(sprite);
//						System.out.println(sprite);
						break;
					}
				}
			}
			else
			{
				if(isHuong)
				this.sprite.setPosition(getX()+45,getY()+10);
				else
					this.sprite.setPosition(getX()-15,getY()+10);	
			if(!this.isAnimationRunning() || !this.isVisible())  {
				isCarry = false;
				isDetach = false;
				sprite.setVisible(false);
//				GameScene.sceneHUD.detachChild(sprite);
			}
			}
			
			if(!GameScene.isFrzee && !isCheckMouseRun){
				if(end1 > start1)
					if(getX() > end1 || getX() < start1 ){
						speed = -speed;
						isHuong = !isHuong;
						if(isHuong) 
							animate(new long[] { 100L, 100L}, new int[]{0,1}, true);
						 else     
							 animate(new long[] { 100L, 100L}, new int[]{2,3}, true);
					}
					if(end1 < start1){
						if(getX() < end1 || getX() > start1 ){
							speed = -speed;
							isHuong = !isHuong;
							if(!isHuong) 
								animate(new long[] { 100L, 100L}, new int[]{0,1}, true);
							 else      
								 animate(new long[] { 100L, 100L}, new int[]{2,3}, true);
						}	
					}
				setPosition(getX()+speed, getY());
			}
			
			
		}
		  @Override
		public void detachMouseGold() {
			// TODO Auto-generated method stub
			super.detachMouseGold();
			if(isDetach){
			isDetach = false;
			sprite.setVisible(false);
			}
		}
		@Override
			public void pullBackGold(float paramFloat1, float paramFloat2,
					float paramFloat3, float paramFloat4, boolean paramBoolean) {
				// TODO Auto-generated method stub
				super.pullBackGold(paramFloat1, paramFloat2, paramFloat3, paramFloat4,
						paramBoolean);
				stopAnimation();
		    	unPigRun();
		    	 isCheckMouseRun = true;
			}
			private SequenceEntityModifier lemRun;
			public Mouse(float pX, float pY, float pWidth, float pHeight,
					ITiledTextureRegion pTiledTextureRegion,
					VertexBufferObjectManager pVertexBufferObjectManager, int paramInt,
					float paramFloat5, String paramString) {
				super(pX, pY, pWidth, pHeight, pTiledTextureRegion, pVertexBufferObjectManager,
						paramInt, paramFloat5, paramString);
				// TODO Auto-generated constructor stub
				 isCarry = false;
				 isDetach = false;
						 vbom = pVertexBufferObjectManager;
						 value = -1;
						 left = false;
						 right = false;
						 isCheckMouseRun = false;
			}
			//huong = true phai sang trai
			public void pigRun(final float paramFloat1, final float paramFloat2, final float paramFloat3, final float paramFloat4, final float paramFloat5, final boolean huong)
			  {
				 end1 = paramFloat3;
				 end2 = paramFloat4;
				 speed = paramFloat5;
				 start1 = paramFloat1;
				 start2 = paramFloat2;
				 isHuong = huong;
				 if(huong) animate(new long[] { 100L, 100L}, new int[]{0,1}, true);
				 else      animate(new long[] { 100L, 100L}, new int[]{2,3}, true);
				 
			  }
			 public void unPigRun()
			  {
			    unregisterEntityModifier(this.lemRun);
			  }
		 

	
}
